package com.enjoyor.soa.traffic.frame.logger.impl;

import com.enjoyor.filter.SSOClientfilter;
import com.enjoyor.soa.traffic.frame.domain.PubLogNorm;
import com.enjoyor.soa.traffic.frame.logger.IPubLogNormMapper;
import com.enjoyor.soa.traffic.frame.security.bean.pojo.UserPojo;
import com.enjoyor.soa.traffic.frame.support.mybatis.mapper.impl.BaseMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.JsonHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class PubLogNormMapper extends BaseMapper<PubLogNorm> implements
        IPubLogNormMapper {

    @Override
    public void insert(PubLogNorm log) {
        log.setLogId(String.valueOf(System.nanoTime()));
        log.setLogTime(new Date());
        super.insert(log);
    }

    @Override
    public void saveNoramlLogInfo(PubLogNorm log) {
        this.insert(log);
    }

    @Override
    public PubLogNorm getBaseLogInfo(String sysName, String logModular, String token, String transType) throws NullPointerException {
        PubLogNorm log = new PubLogNorm();
        UserPojo user = null;
        if (token != null && !token.isEmpty()) {
            user = this.getCurrentUser(token);
        } /*else
            throw new NullPointerException("token为空");*/
        switch (transType) {
            case "select": {
                log.setTransType(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_SELECT
                        .getCode());
                log.setTransTypeName(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_SELECT
                        .getMessage());
                break;
            }
            case "insert": {
                log.setTransType(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT
                        .getCode());
                log.setTransTypeName(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT
                        .getMessage());
                break;
            }
            case "update": {
                log.setTransType(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE
                        .getCode());
                log.setTransTypeName(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE
                        .getMessage());
                break;
            }
            case "delete": {
                log.setTransType(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE
                        .getCode());
                log.setTransTypeName(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE
                        .getMessage());
                break;
            }
            case "export": {
                log.setTransType(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_EXPORT
                        .getCode());
                log.setTransTypeName(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_EXPORT
                        .getMessage());
                break;
            }
        }
        log.setLogIp(user != null ? user.getAddrIP() : "");
        log.setLogUser(user != null ? user.getSsoUserName() : "");
        log.setPersonId(user != null ? user.getPersonId() : "");
        log.setOrgId(user != null ? user.getOrgId() : "");
        log.setOrgName(user != null ? user.getOrgName() : "");
        log.setLogModular(logModular);
        log.setLogMac(user != null ? user.getAddrMAC() : "");
        log.setLogSysName(sysName);
        log.setLogId(TimeHelper.getUniqueId(8));
        log.setLogLevel(LogInfoDic.LOG_LEVEL_ENUM.LOG_NORMAL.getCode());
        log.setLogLevelName(LogInfoDic.LOG_LEVEL_ENUM.LOG_NORMAL.getMessage());
        log.setLogClass(LogInfoDic.LOG_CLASS_ENUM.OPERATOR.getCode());
        log.setLogClassName(LogInfoDic.LOG_CLASS_ENUM.OPERATOR.getMessage());
        return log;
    }

    public void saveLogInfo(String sysName, String logModular, String token,
                            LogInfoDic.LOG_TRANSTYPE_ENUM transType, String content,
                            String result, String contentBefore, String contentAfter) {
        PubLogNorm log = this.getBaseLogInfo(sysName, logModular, token,
                transType);
        log.setContentBefore(contentBefore);
        log.setContent(content);
        log.setContentAfter(contentAfter);
        log.setLogResult(result);
        this.insert(log);
    }

    @Override
    public void saveLogInfo(String sysName, String logModular, UserPojo user,
                            LogInfoDic.LOG_TRANSTYPE_ENUM transType, String content,
                            String result, String contentBefore, String contentAfter) {
        PubLogNorm log = this.getBaseLogInfo(sysName, logModular, user, transType);
        log.setContentBefore(contentBefore);
        log.setContent(content);
        log.setContentAfter(contentAfter);
        log.setLogResult(result);
        this.insert(log);
    }

    @Override
    public PubLogNorm getLogInfo(String sysName, String logModular, Object userDto, String transType) {
        return null;
    }

    private PubLogNorm getBaseLogInfo(String sysName, String logModular,
                                      String token, LogInfoDic.LOG_TRANSTYPE_ENUM transType) {
        PubLogNorm log = new PubLogNorm();
        UserPojo user = new UserPojo();
        if (token != null && !token.isEmpty()) {
            user = this.getCurrentUser(token);
            log.setLogIp(null == user ? "" : user.getAddrIP());
            log.setLogUser(null == user ? "" : user.getSsoUserName());
            log.setPersonId(null == user ? "" : user.getPersonId());
            log.setOrgId(null == user ? "" : user.getOrgId());
            log.setOrgName(null == user ? "" : user.getOrgName());
            log.setLogMac(null == user ? "" : user.getAddrMAC());
        }
        log.setTransType(transType.getCode());
        log.setTransTypeName(transType.getMessage());

        log.setLogModular(logModular);
        log.setLogSysName(sysName);
        log.setLogId(TimeHelper.getUniqueId(8));
        log.setLogLevel(LogInfoDic.LOG_LEVEL_ENUM.LOG_NORMAL.getCode());
        log.setLogLevelName(LogInfoDic.LOG_LEVEL_ENUM.LOG_NORMAL.getMessage());
        log.setLogClass(LogInfoDic.LOG_CLASS_ENUM.OPERATOR.getCode());
        log.setLogClassName(LogInfoDic.LOG_CLASS_ENUM.OPERATOR.getMessage());
        return log;
    }

    private PubLogNorm getBaseLogInfo(String sysName, String logModular,
                                      UserPojo user, LogInfoDic.LOG_TRANSTYPE_ENUM transType) {
        PubLogNorm log = new PubLogNorm();
        log.setLogIp(null == user ? "" : user.getAddrIP());
        log.setLogUser(null == user ? "" : user.getSsoUserName());
        log.setPersonId(null == user ? "" : user.getPersonId());
        log.setOrgId(null == user ? "" : user.getOrgId());
        log.setOrgName(null == user ? "" : user.getOrgName());
        log.setLogMac(null == user ? "" : user.getAddrMAC());
        log.setTransType(transType.getCode());
        log.setTransTypeName(transType.getMessage());
        log.setLogModular(logModular);
        log.setLogSysName(sysName);
        log.setLogId(TimeHelper.getUniqueId(8));
        log.setLogLevel(LogInfoDic.LOG_LEVEL_ENUM.LOG_NORMAL.getCode());
        log.setLogLevelName(LogInfoDic.LOG_LEVEL_ENUM.LOG_NORMAL.getMessage());
        log.setLogClass(LogInfoDic.LOG_CLASS_ENUM.OPERATOR.getCode());
        log.setLogClassName(LogInfoDic.LOG_CLASS_ENUM.OPERATOR.getMessage());
        return log;
    }

    @Override
    public PubLogNorm getBaseLogInfo(String sysName, String logModular,
                                     String transType) throws NullPointerException {
        PubLogNorm log = new PubLogNorm();
        switch (transType) {
            case "select": {
                log.setTransType(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_SELECT.getCode());
                log.setTransTypeName(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_SELECT.getMessage());
                break;
            }
            case "insert": {
                log.setTransType(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode());
                log.setTransTypeName(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getMessage());
                break;
            }
            case "update": {
                log.setTransType(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode());
                log.setTransTypeName(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getMessage());
                break;
            }
            case "delete": {
                log.setTransType(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode());
                log.setTransTypeName(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getMessage());
                break;
            }
            case "export": {
                log.setTransType(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_EXPORT.getCode());
                log.setTransTypeName(LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_EXPORT.getMessage());
                break;
            }
        }
        log.setLogModular(logModular);
        log.setLogSysName(sysName);
        log.setLogId(TimeHelper.getUniqueId(8));
        log.setLogLevel(LogInfoDic.LOG_LEVEL_ENUM.LOG_NORMAL.getCode());
        log.setLogLevelName(LogInfoDic.LOG_LEVEL_ENUM.LOG_NORMAL.getMessage());
        log.setLogClass(LogInfoDic.LOG_CLASS_ENUM.OPERATOR.getCode());
        log.setLogClassName(LogInfoDic.LOG_CLASS_ENUM.OPERATOR.getMessage());
        return log;
    }

    private UserPojo getCurrentUser(String token) {
        String authorizeStr = JsonHelper.beanToJsonStr(SSOClientfilter.getUserInfo(token));
        if (authorizeStr != null && !StringUtils.isBlank(authorizeStr)) {
            UserPojo currentUser = (UserPojo) JsonHelper.jsonToObject(
                    authorizeStr, UserPojo.class);
            return currentUser;
        }
        return null;
    }
}
